<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @close="clearData">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="150px">
      <el-form-item label="sku_id" prop="skuId">

        <select-sku-by-name-component @update-sku-id="updateSkuId"
                                      ref="selectSkuByNameComponentRef"></select-sku-by-name-component>

        <!--      <el-select
                v-model="dataForm.skuId"
                filterable
                remote
                reserve-keyword
                placeholder="请输入要搜索的sku名"
                :remote-method="searchSkuNameHandler"
                :loading="loading">
                <el-option
                  v-for="(item,index) in skuList"
                  :key="index"
                  :label="item.skuName"
                  :value="item.skuId">
                </el-option>
              </el-select>-->
        <!--      <el-input v-model="dataForm.skuId" placeholder="sku_id" disabled></el-input>-->
      </el-form-item>
      <el-form-item label="仓库id" prop="wareId">

        <select-ware-by-name-component @update-ware-id="updateWareId"
                                       ref="selectWareByNameComponentRef">
        </select-ware-by-name-component>

        <!--      <el-input v-model="dataForm.wareId" placeholder="仓库id" disabled></el-input>-->
        <!--      <el-select
                v-model="dataForm.wareId"
                filterable
                remote
                reserve-keyword
                placeholder="请输入要搜索的仓库信息"
                :remote-method="searchSkuNameHandler"
                :loading="loading">
                <el-option
                  v-for="(item,index) in wareList"
                  :key="index"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>-->
      </el-form-item>
      <el-form-item label="库存数" prop="stock">
        <el-input v-model="dataForm.stock" placeholder="库存数"></el-input>
      </el-form-item>
      <el-form-item label="sku_name" prop="skuName">
        <el-input v-model="dataForm.skuName" placeholder="sku_name"></el-input>
      </el-form-item>
      <el-form-item label="锁定库存" prop="stockLocked">
        <el-input v-model="dataForm.stockLocked" placeholder="锁定库存"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import skuApi from '../../../../api/product/skuApi'
import wareInfoApi from '../../../../api/ware/wareInfoApi'

export default {
  components: {
    'select-ware-by-name-component': () => import('../wareInfoComponent/select-ware-by-name-component'),
    'select-sku-by-name-component': () => import('./select-sku-by-name-component'),
  },
  data () {
    return {
      visible: false,
      dataForm: {
        id: null,
        skuId: '',
        wareId: '',
        stock: '',
        skuName: '',
        stockLocked: ''
      },
      dataRule: {
        skuId: [
          {required: true, message: 'sku_id不能为空', trigger: 'blur'}
        ],
        wareId: [
          {required: true, message: '仓库id不能为空', trigger: 'blur'}
        ],
        stock: [
          {required: true, message: '库存数不能为空', trigger: 'blur'}
        ],
        skuName: [
          {required: true, message: 'sku_name不能为空', trigger: 'blur'}
        ],
        stockLocked: [
          {required: true, message: '锁定库存不能为空', trigger: 'blur'}
        ]
      },
      skuPageInfo: {
        currentIndex: 1,
        size: 10,
        totalCount: 0,
        totalPage: 0
      },
      skuList: [],
      loading: false,
      skuName: null,
      warePageInfo: {
        currentIndex: 1,
        size: 10,
        totalCount: 0,
        totalPage: 0
      },
      wareList: [],
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/wms/waresku/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.skuId = data.wareSku.skuId
              this.dataForm.wareId = data.wareSku.wareId
              this.dataForm.stock = data.wareSku.stock
              this.dataForm.skuName = data.wareSku.skuName
              this.dataForm.stockLocked = data.wareSku.stockLocked
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/wms/waresku/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'skuId': this.dataForm.skuId,
              'wareId': this.dataForm.wareId,
              'stock': this.dataForm.stock,
              'skuName': this.dataForm.skuName,
              'stockLocked': this.dataForm.stockLocked
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    async getSkuList (skuName) {
      let p = await skuApi.list(this.skuPageInfo.currentIndex, this.skuPageInfo.size, skuName)

      this.skuPageInfo.totalCount = p.data.page.totalCount

      this.skuPageInfo.totalPage = p.data.page.totalPage

      this.skuList = p.data.page.list
    },

    searchSkuNameHandler (skuName) {
      this.getSkuList(skuName)
    },
    async searchWareInfoHandler (wareInfoKey) {
      let p = await wareInfoApi.getWareList(this.skuPageInfo.currentIndex, this.skuPageInfo.size, wareInfoKey)
      this.wareList = p.data.page.list

      console.log(this.wareList)

      this.warePageInfo.totalCount = p.data.page.totalCount

      this.warePageInfo.totalPage = p.data.page.totalPage
    },
    updateSkuId (skuId) {
      this.dataForm.skuId = skuId
    },
    updateWareId (wareId) {
      this.dataForm.wareId = wareId
    },
    clearData () {
      this.dataForm = {
        id: null,
        skuId: '',
        wareId: '',
        stock: '',
        skuName: '',
        stockLocked: ''
      }
    }
  },
  created () {
    this.getSkuList()
    this.searchWareInfoHandler()
  },
  watch: {
    'dataForm.skuId' (val) {
      console.log('skuId 更新了', val)

      if (val) {

        skuApi.getSkuInfo(val).then(({data}) => {

          this.dataForm.skuName = data.skuInfo.skuName
        })
      }

    }
  }
}
</script>
